using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WPCoco;
using WPCoco.Particles;
using Microsoft.Xna.Framework.Input.Touch;
using WPCoco.Controllers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using WPCoco.Graphics;
using Microsoft.Devices.Sensors;
using System.Diagnostics;

namespace ParticlesDemo.Scenes
{
    public class BoilingFoamScene : DemoScene
    {
        public BoilingFoamScene()
        {
            Title = "Boiling Foam + Accelerometer";

            IsRunningChange += new EventHandler(MainScene_IsRunningChange);

            ParticleSystem p = ParticleSystem.LoadFromFile("BoilingFoam");
            p.SourcePosition = Screen.Center;
            pnode = new ParticleSystemNode(p);
            AddNode(pnode);

            acc.CurrentValueChanged += (o, e) => 
            {
                /*
                float angle = (float)Math.Atan2(e.SensorReading.Acceleration.X, e.SensorReading.Acceleration.Y);
                angle = MathHelper.ToDegrees(angle);
                p.ParticleAngle = angle;
                */
                p.GravitySystemData.Gravity = new Vector2(e.SensorReading.Acceleration.Y, e.SensorReading.Acceleration.X) * 200;
            };
        }

        Accelerometer acc = new Accelerometer();

        void MainScene_IsRunningChange(object sender, EventArgs e)
        {
            if (IsRunning)
            {
                acc.Start();
                ControllerManager.Shared.TouchScreen.Dragging += new EventHandler<CustomEventArgs<GestureSample>>(TouchScreen_Dragging);
            }
            else
            {
                acc.Stop();
                ControllerManager.Shared.TouchScreen.Dragging -= new EventHandler<CustomEventArgs<GestureSample>>(TouchScreen_Dragging);
            }
        }

        ParticleSystemNode pnode;

        void TouchScreen_Dragging(object sender, CustomEventArgs<GestureSample> e)
        {
            pnode.System.SourcePosition = e.Value.Position;
        }
    }
}
